/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package listener;

import bean.UserRandom;
import common.Constant;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 *
 * @author root
 */
public class RandomListener implements ServletContextListener{

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        Timer timer = new Timer();
//        timer.schedule(new MyTimerTask(), 5*60*1000, 5*60*1000);
        long period = 5*60*1000;
        timer.schedule(new MyTimerTask(), 0,  period);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
    
    class MyTimerTask extends TimerTask{

        @Override
        public void run() {
            if(Constant.phoneRandom!=null){
                System.out.println("MyTimerTask run");
                Iterator<Map.Entry<String,UserRandom>> it = Constant.phoneRandom.entrySet().iterator();
                while(it.hasNext()){
                    Map.Entry<String,UserRandom> entry = it.next();
                    long tmp = System.currentTimeMillis()-entry.getValue().getTimeStamp();
                    if(tmp>1000*60*10){
                        Constant.phoneRandom.remove(entry.getKey());
                    }
                }
            }
        }
    }
}
